package log350.tp3;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import classes.User;
import classes.UserHandler;
import android.graphics.Color;
import android.graphics.Paint;
import android.hardware.Camera.Size;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class ListeParticipantsActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_liste_participants);
       
        buildListParticipants(UserHandler.getInstance().getAllUser());
    }

	private void buildListParticipants(ArrayList<User> rows) {
		TableLayout tableLayoutLink = (TableLayout)findViewById(R.id.tblLink);
        tableLayoutLink.removeAllViews();
        
        TableRow tableRowLink = new TableRow(getApplicationContext());            
        
        final TextView lanETS = new TextView(getApplicationContext());
        lanETS.setText("Lan ETS");  
        lanETS.setTextColor(0xFFff8000);
        lanETS.setTextSize(12);
        lanETS.setPaintFlags( lanETS.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
       
        lanETS.setPadding(15, 10, 0, 20);
        lanETS.setClickable(true);
        lanETS.setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
				setResult(RESULT_OK, new Intent());
		        finish();
			}

          });
        tableRowLink.addView(lanETS);
        
        final TextView firstComma = new TextView(getApplicationContext());
        firstComma.setText(">");
        firstComma.setTextSize(12);
        firstComma.setPadding(10, 10, 0, 20);
        tableRowLink.addView(firstComma);
        
        final TextView listeParticipant = new TextView(getApplicationContext());
        listeParticipant.setText("Liste des participants");   
        listeParticipant.setTextSize(12);        
        listeParticipant.setPadding(10, 10, 0, 20);
        tableRowLink.addView(listeParticipant);
        
        tableLayoutLink.addView(tableRowLink);
		
		TableLayout tableLayout = (TableLayout)findViewById(R.id.tab);
        tableLayout.removeAllViews();
        
        TableRow tableRowTop = new TableRow(getApplicationContext());            
        
        final TextView columsTopUsername = new TextView(getApplicationContext());
        columsTopUsername.setText("Nom d'utilisateur");  
        columsTopUsername.setTextColor(0xFFff8000);
        columsTopUsername.setTextSize(15);
    	columsTopUsername.setBackgroundColor(Color.argb(50, 205, 205, 193));   
        columsTopUsername.setPaintFlags( columsTopUsername.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
       
        columsTopUsername.setPadding(20, 10, 20, 30);
        columsTopUsername.setClickable(true);
        columsTopUsername.setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
				ArrayList<User> listUser = UserHandler.getInstance().getAllUser();
				Collections.sort(listUser, User.COMPARE_BY_USERNAME);
				buildListParticipants(listUser);
			}

          });
        tableRowTop.addView(columsTopUsername);
        
        final TextView columsTopName = new TextView(getApplicationContext());
        columsTopName.setText("Nom");   
        columsTopName.setTextColor(0xFFff8000); 
        columsTopName.setTextSize(15);       
    	columsTopName.setBackgroundColor(Color.argb(50, 205, 205, 193));   
        columsTopName.setPaintFlags( columsTopName.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
        
        columsTopName.setPadding(10, 10, 20, 30);
        columsTopName.setOnClickListener(new OnClickListener(){
     			public void onClick(View arg0) {
     				ArrayList<User> listUser = UserHandler.getInstance().getAllUser();
     				Collections.sort(listUser, User.COMPARE_BY_NAME);
     				buildListParticipants(listUser);
     			}

               });                
        tableRowTop.addView(columsTopName);
        
        tableLayout.addView(tableRowTop);
        
        for(int i=0;i<rows.size();i++){
            User row  = rows.get(i);
            TableRow tableRow = new TableRow(getApplicationContext());            
            
            final TextView columsViewUsername = new TextView(getApplicationContext());
            columsViewUsername.setText(row.getUsername());
            columsViewUsername.setPadding(20, 10, 10, 0);    
            if(i %2 !=0)
            	columsViewUsername.setBackgroundColor(Color.argb(50, 205, 205, 193));
            tableRow.addView(columsViewUsername);
            
            final TextView columsViewName = new TextView(getApplicationContext());
            columsViewName.setText(row.getName());
            columsViewName.setPadding(10, 10, 10, 0);
            if(i %2 !=0)
            	columsViewName.setBackgroundColor(Color.argb(50, 205, 205, 193));
            tableRow.addView(columsViewName);
            
            tableLayout.addView(tableRow);
        }
	}
	

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_liste_participants, menu);
        return true;
    }
    
    
}
